
from meta_service.store import GetAllEntryIds
from meta_service.store import ArxivPaper

def KeysInPaper(keys:list[str],entryIds:set[str]):
    selected_entryIds = set()
    for entry_id in entryIds:
        entry = ArxivPaper(entry_id)
        if entry is None:
            print(entry_id,"not found")
            continue

        summary = entry.get_summary()
        title = entry.get_title()
        text_all = title + " " + summary
        text_all = str(text_all).lower()
        for key in keys:
            key = key.lower()
            if key in text_all:
                selected_entryIds.add(entry_id)
                break
    return selected_entryIds

# # 每个 keys 中只要有一个关键词出现在文本中,就认为是符合条件的
# keys1 = [ "hallucination", "hallucinate", "hallucinat" ]
# keys2 = [ "token-level", "token level" , "token" ]
# keys3 = [ "mitigating" , "mitigate" , "mitigation" ]

# T = [
#     keys1,
#     keys2,
#     keys3
# ]

def PipeSearchInYear2024Papers(T:list[list[str]]):
    all = GetAllEntryIds()
    all = [ i for i in all if i.startswith("24") ]
    all = sorted(all,reverse=True)
    print(len(all))

    papers = set(all)
    for keys in T:
        papers = KeysInPaper(keys,papers)
        print(len(papers))
    papers = sorted(list(papers),reverse=True)

    print(papers)
    print("len(papers):",len(papers))
    return papers


# for entry_id in papers:
#     entry = ArxivPaper(entry_id)
#     print(f"https://arxiv.org/abs/{entry_id}")
#     print(entry.get_title())


